/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aliang.jp.jp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author liuruile
 */
public class Settings implements Serializable {

    public static int IMAGEFORMAT_JPG = 1;
    public static int IMAGEFORMAT_BMP = 2;
    public static int FILE = 1;
    public static int CLIP = 2;
    private String outputDir = getDefaultOutputDir();
    // 图片保存的格式
    private int imageFormat = IMAGEFORMAT_JPG;
    private int savePlace = FILE;
    private int delaytime;
    private static Settings settings;


    static {
        File f = new File("./settings");
        if (f.exists()) {
            ObjectInputStream in = null;
            try {
                in = new ObjectInputStream(new FileInputStream(f));
                settings = (Settings) in.readObject();
            } catch (IOException ex) {
                Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    in.close();
                } catch (IOException ex) {
                    Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        } else {
            settings = new Settings();
        }
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

            public void run() {
                writet2File("./settings");
            }

            private void writet2File(String resource) {
                ObjectOutputStream out = null;
                try {
                    out = new ObjectOutputStream(new FileOutputStream(resource));
                    if (settings != null) {
                        out.writeObject(settings);
                    }
                    out.close();
                } catch (IOException ex) {
                    Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    try {
                        out.close();
                    } catch (IOException ex) {
                        Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }));
    }

    public int getSavePlace() {
        return savePlace;
    }

    public void setSavePlace(int savePlace) {
        this.savePlace = savePlace;
    }

    public int getImageFormat() {
        return imageFormat;
    }

    public void setImageFormat(int imageFormat) {
        this.imageFormat = imageFormat;
    }

    public void setOutputDir(String outputDir) {
        this.outputDir = outputDir;
    }

    public static Settings getInstance() {
        return settings;
    }

    public String getOutputFileName() {
        String path = getOutputDir();
        StringBuilder sb = new StringBuilder(path);
        sb.append(File.separator);
        sb.append("screen");
        Calendar cal = Calendar.getInstance();
        sb.append(cal.get(Calendar.HOUR_OF_DAY));
        sb.append('-');
        sb.append(cal.get(Calendar.MINUTE));
        sb.append('-');
        sb.append(cal.get(Calendar.SECOND));
        sb.append(".");
        sb.append(getImageFormatString());
        String fileName = sb.toString();
        return fileName;
    }

    public String getImageFormatString() {
        if (getImageFormat() == Settings.IMAGEFORMAT_BMP) {
            return "bmp";
        } else {
            return "jpg";
        }
    }

    public String getOutputDir() {
        // TODO Auto-generated method stub
        return outputDir;
    }

    public void setDelaytime(String trim) {
        delaytime = Integer.parseInt(trim);
    }

    public int getDelaytime()
    {
        return delaytime;
    }

    private String getDefaultOutputDir() {
        StringBuilder sb = new StringBuilder(System.getProperty("user.home"));
        sb.append(File.separator);
        sb.append("Desktop");
        return sb.toString();
    }
}
